package com.dc.toolkit.hutool.json;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * Description: Hutool JsonUtil Demo
 * @author: gdc
 * @date: 2021/5/9
 * @version 1.0
 */
public class JSONUtilTest {

    /**
     * Json 对象字符串
     */
    private String JSON_STR_FISH = "{\"weight\":3.5,\"name\":\"鲤鱼\",\"age\":2}";
    /**
     * Json Object
     */
    private JSONObject JSONOBJECT_FISH = JSONUtil.createObj().set("name", "鲶鱼").set("weight", 5.7).set("age", 6);
    /**
     * Json 字符串列表
     */
    private String JSON_STR_FISH_LIST = "[{\"weight\":3.5,\"name\":\"鲤鱼\",\"age\":2},{\"weight\":6.7,\"name\":\"鲶鱼\",\"age\":5},{\"weight\":4.1,\"name\":\"带鱼\",\"age\":3}]";
    /**
     * 对象(不存在空值)
     */
    private Fish FISH = new Fish("鲤鱼", 3.5, 2);
    /**
     * 对象(存在空值)
     */
    private Fish FISH_EXISH_NULL = new Fish("鲤鱼", 3.5, null);
    /**
     * 对象列表
     */
    private List<Fish> FISH_LIST = Arrays.asList(new Fish("鲤鱼", 3.5, 2),
            new Fish("鲶鱼", 6.7, 5),
            new Fish("带鱼", 4.1, 3));
    /**
     * 对象列表(存在空值)
     */
    private List<Fish> FISH_LIST_EXIST_NULL = Arrays.asList(new Fish(null, 3.5, 2),
            new Fish("鲶鱼", 6.7, null),
            new Fish("带鱼", null, 3));
    /**
     * 对象(存在空值)
     */
    private Map MAP_FISH = new HashMap(){{
        put("name", "鲶鱼");
        put("age", 6);
    }};

    /**
     * 测试   创建空的 JSON object 和 Array
     */
    @Test
    public void testCreateObjOrArray(){
        JSONObject jsonObject = JSONUtil.createObj();
        System.out.println(jsonObject);
        JSONArray jsonArray = JSONUtil.createArray();
        System.out.println(jsonArray);
    }


    /**
     * 测试   对象转 JSON 字符串
     */
    @Test
    public void testToJsonStr(){

        String jsonStr = JSONUtil.toJsonStr(FISH);
        System.out.println(jsonStr);

        String jsonListStr = JSONUtil.toJsonStr(FISH_LIST);
        System.out.println(jsonListStr);
    }

    /**
     * 测试  字符串 转 Bean
     */
    @Test
    public void testStrToBean(){
        Fish fish = JSONUtil.toBean(JSON_STR_FISH, Fish.class);
        System.out.println(fish);
    }

    /**
     * 测试  JsonObject 转 Bean
     */
    @Test
    public void testJsonObjectToBean(){
        Fish fish = JSONUtil.toBean(JSONOBJECT_FISH, Fish.class);
        System.out.println(fish);
    }

    /**
     * 测试  列表字符串 转 List
     *
     */
    @Test
    public void testStrToList(){
        List<Fish> fishList = JSONUtil.toList(JSON_STR_FISH_LIST, Fish.class);
        System.out.println(fishList);
    }

    /**
     * 测试  JSONArray 转 List
     */
    @Test
    public void testJsonObjectToList(){
        JSONArray jsonArray = JSONUtil.parseArray(JSON_STR_FISH_LIST);
        List<Fish> fishList = JSONUtil.toList(jsonArray, Fish.class);
        System.out.println(fishList);
    }

    /**
     * 测试   对象、对象字符串、列表字符串、对象列表 转 JSON
     */
    @Test
    public void testParse(){
        JSON json1 = JSONUtil.parse(JSON_STR_FISH_LIST);
        System.out.println(json1);

        JSON json2 = JSONUtil.parseArray(FISH_LIST);
        System.out.println(json2);

        JSON json3 = JSONUtil.parse(JSON_STR_FISH);
        System.out.println(json3);

        JSON json4 = JSONUtil.parse(FISH);
        System.out.println(json4);
    }

    /**
     * 测试   对象字符串、对象 转 JSONObject
     */
    @Test
    public void testParseObj(){
        JSONObject jsonObj1 = JSONUtil.parseObj(JSON_STR_FISH);
        System.out.println(jsonObj1);

        JSONObject jsonObj2 = JSONUtil.parseObj(FISH);
        System.out.println(jsonObj2);
    }

    /**
     * 测试   对象字符串、对象 转 JSONObject(不忽略空值)
     */
    @Test
    public void testParseObj_ignore_nullValue(){
        JSONObject jsonObj1 = JSONUtil.parseObj(FISH_EXISH_NULL);
        System.out.println(jsonObj1);

        JSONObject jsonObj2 = JSONUtil.parseObj(FISH_EXISH_NULL, false);
        System.out.println(jsonObj2);
    }

    /**
     * 测试   列表字符串、对象列表 转 JSONArray
     */
    @Test
    public void testParseArray(){
        JSONArray jsonArray = JSONUtil.parseArray(JSON_STR_FISH_LIST);
        System.out.println(jsonArray);

        JSONArray jsonArray2 = JSONUtil.parseArray(FISH_LIST);
        System.out.println(jsonArray2);
    }

    /**
     * 测试   字符串列表、对象列表 转 JsonArray
     *  默认忽略空值，如果保留空值，设置为false
     */
    @Test
    public void testParseArray_ignore_nullValue(){
        JSONArray jsonArray = JSONUtil.parseArray(FISH_LIST_EXIST_NULL);
        System.out.println(jsonArray);

        JSONArray jsonArray2 = JSONUtil.parseArray(FISH_LIST_EXIST_NULL, false);
        System.out.println(jsonArray2);
    }

    /**
     * 测试   JSON 转换为XML字符串
     */
    @Test
    public void testToXmlStr(){
        JSON json = JSONUtil.parse(FISH);
        String xmlStr = JSONUtil.toXmlStr(json);
        System.out.println(xmlStr);
    }

    /**
     * 测试   JSON 字符串是否 为JSON
     */
    @Test
    public void testIsJson(){
        boolean is1 = JSONUtil.isJson(JSON_STR_FISH);
        assertThat(is1, equalTo(true));

        boolean is2 = JSONUtil.isJson(JSON_STR_FISH_LIST);
        assertThat(is2, equalTo(true));

        boolean is3 = JSONUtil.isJson("111111111");
        assertThat(is3, equalTo(false));
    }


    /**
     * 测试   JSON 字符串是否 为JSONObj
     */
    @Test
    public void testIsJsonObj(){
        boolean is1 = JSONUtil.isJsonObj(JSON_STR_FISH);
        assertThat(is1, equalTo(true));

        boolean is2 = JSONUtil.isJsonObj(JSON_STR_FISH_LIST);
        assertThat(is2, equalTo(false));

        boolean is3 = JSONUtil.isJsonObj("111111111");
        assertThat(is3, equalTo(false));
    }

    /**
     * 测试     JSON 字符串是否 为JSONArray
     */
    @Test
    public void testIsJsonArray(){
        boolean is1 = JSONUtil.isJsonArray(JSON_STR_FISH);
        assertThat(is1, equalTo(false));

        boolean is2 = JSONUtil.isJsonArray(JSON_STR_FISH_LIST);
        assertThat(is2, equalTo(true));

        boolean is3 = JSONUtil.isJsonArray("111111111");
        assertThat(is3, equalTo(false));
    }


    /**
     * 测试 鱼 类
     */
    @Data
    @AllArgsConstructor
    static class Fish{
        /**
         * 名称
         */
        private String name;
        /**
         * 重量
         */
        private Double weight;
        /**
         * 多少年
         */
        private Integer age;

    }
}



